﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import Namespace="Equipment.DataAccess" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create Email Notification
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CurrentMenuIDContent" runat="server">
    profileMenu</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <link rel="stylesheet" type="text/css" href="../../Content/jquery-ui-1.8.16.custom.css" />
    <link rel="stylesheet" type="text/css" href="../../Content/jquery.autocomplete.css" />
    <script type="text/javascript" src="../../Scripts/site.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery-ui-1.8.16.custom.min.js"></script>
    <script src="<%= Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%  Dim un As Equipment.DataAccess.UserNotification = DirectCast(ViewData("currNotification"), Equipment.DataAccess.UserNotification)%>
    <h2>
       <%: ViewData("Mode")%> Email Notification</h2>
    <br />
    <fieldset>
        <legend>Create Notification</legend>
        <br />
        <form id="notificationForm" method="post" action="<%= Html.AttributeEncode(Url.Action("Save")) %>">
        <div id="wizardContent">
            <div id="step1Content">
                <table border="1">
                    <tr>
                        <td width="120px" rowspan="2">
                            <img border="0" src="../../Content/img/email-notification.png" width="80" height="80">
                        </td>
                        <td width="130px">
                            <label for="Id">
                                Notification Name:</label>
                        </td>
                        <td width="280px">
                            <%= Html.TextBox("txtNotificationName", un.Description, New With {.class = "text"})%>
                        </td>
                        <td>
                        </td>
                    </tr>
                    <tr>
                        <td class="top">
                            <label for="Id">
                                Trigger:</label>
                        </td>
                        <td class="top">
                            <%= Html.DropDownList("Trigger")%>
                        </td>
                        <td>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            &nbsp;
                        </td>
                        <td>
                            <input type="submit" class="button right" value="Next >>" onclick="toOverdueStep(false); return false;" />
                        </td>
                        <td>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="step2Content" class="hidden">
            </div>
            <div id="step3Content" class="hidden">
            </div>
        </div>
        </form>
    </fieldset>
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
    <script type="text/javascript">

        function toTypeStep(back) {
            if (back) {
                saveOverdue();
            }

            $("#step2Content").hide();
            $("#step3Content").hide();
            $("#step1Content").fadeIn(500);
        }

        function saveType() {

            var trigger = $("#Trigger").val();
            var name = $("#txtNotificationName").val();

            jQuery.ajax({
                type: 'POST',
                data: { trigger: trigger, name: name },
                url: '<%= Html.AttributeEncode(Url.Action("SaveType")) %>',
                async: false,
                success: function (result) {
                }
            });
        }

        function toOverdueStep(back) {

            if (back) {
                if (saveDepartment() === false) {
                    return;
                }
            }
            else {
                if (nameValidate() === false)
                    return;
                saveType();
            }

            var trigger = $("#Trigger").val();
            var url = '<%= Html.AttributeEncode(Url.Action("DepartmentStep")) %>';
            if (trigger === "WorkOrder_Overdue_ByDay" || trigger === "WorkOrder_Overdue_ByHour") {
                url = '<%= Html.AttributeEncode(Url.Action("OverdueStep")) %>';
            }

            jQuery.ajax({
                type: 'POST',
                data: {},
                url: url,
                success: function (result) {
                    $("#step1Content").hide();
                    $("#step3Content").hide();
                    $("#step2Content").html(result).fadeIn(500);
                    if (trigger != "WorkOrder_Overdue_ByDay" && trigger != "WorkOrder_Overdue_ByHour") {
                        $("select#Site").change(function () {
                            loadDepartmentList();
                        });
                    }
                }
            });
        }

        function saveOverdue(overdueType, overdueXValue) {
            jQuery.ajax({
                type: 'POST',
                data: { overdueType: overdueType, overdueXValue: overdueXValue },
                url: '<%= Html.AttributeEncode(Url.Action("saveOverdue")) %>',
                async: false,
                success: function (result) {

                }
            });
        }

        function toDepartmentStep(back) {

            var trigger = $("#Trigger").val();

            if (trigger === "WorkOrder_Overdue_ByDay" || trigger === "WorkOrder_Overdue_ByHour") {

                if (!back) {
                    var radioVal = $("input[name='rbOverdueType']:checked").val();
                    var overdueVal = 0;

                    if (radioVal === "Before") {
                        overdueVal = $("#txtOverdueBefore").val();
                        saveOverdue("<%=NotificationOverdueType.XBeforeOverdue.ToString() %>", overdueVal);
                    }
                    else if (radioVal === "After") {
                        overdueVal = $("#txtOverdueAfter").val();
                        saveOverdue("<%=NotificationOverdueType.EveryXAfterOverdue.ToString() %>", overdueVal);
                    }
                    else {
                        alert('Select Overdue type');
                        return;
                    }
                }
            }

            jQuery.ajax({
                type: 'POST',
                data: {},
                url: '<%= Html.AttributeEncode(Url.Action("DepartmentStep")) %>',
                success: function (result) {
                    $("#step1Content").hide();
                    $("#step2Content").hide();
                    $("#step3Content").html(result).fadeIn(500);
                    $("select#Site").change(function () {
                        loadDepartmentList();
                    });
                }
            });
        }


        function loadDepartmentList() {
            var siteID = $("#Site").val();
            jQuery.ajax({
                type: 'POST',
                data: { siteID: siteID },
                url: '<%= Url.Content("~/SitesDepartmentSetup/GetDepartmentDDBySiteID") %>',
                success: function (result) {
                    $('#departmentContainer').html(result);
                }
            });
        }

        function saveDepartment() {
            var assetTypeID = $("#AssetType").val();
            var locationID = $("#Department").val();
            if (locationID > 0) {
                jQuery.ajax({
                    type: 'POST',
                    data: { locationID: locationID, assetTypeID: assetTypeID },
                    url: '<%= Html.AttributeEncode(Url.Action("SaveDepartment")) %>',
                    async: false,
                    success: function (result) {

                    }
                });
                return true;
            }
            else {
                alert("Please select Department.");
                return false;
            }
        }

        function toPreviewStep(back) {

            if (!back) {
                if (saveDepartment() === false) {
                    return;
                }
            }
            
            jQuery.ajax({
                type: 'POST',
                data: { },
                url: '<%= Html.AttributeEncode(Url.Action("PreviewStep")) %>',
                success: function (result) {
                    $("#step1Content").hide();
                    $("#step2Content").hide();
                    $("#step3Content").html(result).fadeIn(500);
                }
            });
        }

        function save() {
            jQuery.ajax({
                type: 'POST',
                data: {},
                url: '<%= Html.AttributeEncode(Url.Action("Save")) %>',
                async: false,
                success: function (result) {
                    if (result.Result != "ok") {
                        $("#errMsg").html(result.Message);
                    }
                    else {
                        document.location = '<%= Html.AttributeEncode(Url.Action("Index")) %>';
                    }
                }
            });
        }


        function nameValidate() {
            return $('#notificationForm').validate({
                rules: {
                    "txtNotificationName": { required: true }
                },
                messages: {
                    "txtNotificationName": " required field."
                }
            }).form();
        }


    </script>
</asp:Content>
